﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;
using System.Text.RegularExpressions;

namespace DTcms.Web.m
{
    /// <summary>
    /// xinxianshi_save 的摘要说明
    /// </summary>
    public class comment_save : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            BLL.users userbll = new BLL.users();
            //判断用户名密码是否正确
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            int _article_id = DTRequest.GetFormInt("article_id", 0);
            string _remark = DTRequest.GetFormString("txtRemark");
            //int userstate = userbll.CheckUserState(user_name);
            //if (userstate == 3)
            //{
            //    context.Response.Write("{\"status\":0, \"msg\":\"错误提示：该账号已被禁用，无法登录！\"}");
            //    return;
            //}
            Utils.WriteTextLog("comment_save" + user_name, "username:" + user_name + ",pwd:" + user_pwd + "/r/n_article_id："+ _article_id+ ",_remark:"+ _remark, DateTime.Now);
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
           
            if (userModel == null)
            {
                Utils.WriteTextLog("comment_save" + user_name, "username:" + user_name + ",pwd:" + user_pwd + "/r/n userModel is null" , DateTime.Now);
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
          

            if (_article_id == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"未指定ID号！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"请输入您要发布的内容！\"}");
                return;
            }
           
            //开始提交内容
            BLL.xinxi_comment bll = new BLL.xinxi_comment();
            Model.xinxi_comment model = new Model.xinxi_comment();

            model.article_id = _article_id;
            model.remark = _remark;
            model.user_id = userModel.id;
            model.status = 0;
            model.add_time = DateTime.Now;

            int result = bll.Add(model);
            if (result > 0)
            {
                #region  留言@的判断逻辑
                try
                {
                    if (_remark.IndexOf("@") >= 0)
                    {
                        List<string> userat = userlist(_remark);
                        string __remark = "";
                        foreach (var uat in userat)
                        {
                           
                            bool isexuser = new BLL.company().ExistsTitle(uat.Trim());//检查用户名是否存在
                            if (isexuser)
                            {//用户名存在，写入用户提醒
                                var user_id = new BLL.company().GetIDByTitle(uat.Trim());
                                if (user_id > 0)
                                {
                                    BLL.xinxi_comment_at bllat = new BLL.xinxi_comment_at();
                                    Model.xinxi_comment_at modelat = new Model.xinxi_comment_at();
                                    modelat.article_id = _article_id;
                                    modelat.comment_id = result;
                                    modelat.user_id = user_id;
                                    modelat.is_read = 0;
                                    modelat.add_time = DateTime.Now;
                                    bllat.Add(modelat);

                                    //替换 评论中的@ 添加打开店铺黄页按钮
                                    string s = "@" + uat.Trim();
                                    __remark = _remark.Replace(s, "@<a href=\"javascript:void()\" onclick=\"showcompany(\"win_company_index\",\"" + user_id + "\",\"" + uat.Trim() + "\")\">" + uat.Trim()+"</a> ");
                                    Utils.WriteTextLog("comment_save", s + "\r\n"+ __remark, DateTime.Now);
                                }
                            }
                        }
                        bool issuccess = false;
                        int i = 0;
                        int j = __remark.IndexOf("showcompany");
                        if (j > 0) {
                            i = 99;
                            issuccess = bll.UpdateField(result, "remark='" + __remark + "'");
                        }
                        Utils.WriteTextLog("comment_save", __remark+"\r\n"+i+" "+ issuccess+" "+ j, DateTime.Now);

                    }
                }
                catch (Exception ex) { Utils.WriteTextLog("comment_save", ex.Message, DateTime.Now); }
                #endregion

                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，评论提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }


        public List<string> userlist(string _usercon) {
            List<string> a = new List<string>();
           

            Regex reg = new Regex("(?<=@).*?(?= ) ", RegexOptions.IgnoreCase);
            MatchCollection m = reg.Matches(_usercon);
            foreach (Match math in m)
            {
                string imgUrl = math.ToString();
                a.Add(imgUrl);
            }
            
            return a;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}